package Twitter
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.Application;
	
	public class TimedEvents
	{
		public var _use_timed_events:Boolean = true;
		
		public var _message_interval:int 	= 180000; 	//3 mins
		public var _direct_interval:int 	= 420000; 	//7 mins
		public var _replies_interval:int 	= 300000;	//5 mins
		public var _groups_interval:int 	= 180000;
		
		public var twitter:Twitter.Actions = new Actions;
		
		
		public function TimedEvents()
		{
			
			
		}
		
		/// First time
		public function FirstTimeRun():void
		{
			
			var get_first_messages:Timer = new Timer(1000,1);
			
			get_first_messages.addEventListener(TimerEvent.TIMER, onFirstMessagesTimerTick);
			
			get_first_messages.start();
			
			var first_timer:Timer = new Timer (10000, 1);
			
			first_timer.addEventListener(TimerEvent.TIMER, onFirstTimerTick);
			
			first_timer.start();
			
			if (_use_timed_events)
			{
				
				Messages();
				
				ScheduledTweets();
				
				DirectMessage();
				
				//get_replies
				ReplyMessage();
				
				//Get Groups
				Groups();
				
			/*
				
				

				
				//Get Groups
				Application.application.filteractions.get_group_status();
				
				//Get Counters
				Application.application.counter.get_totals();
				
				*/
				
				Keywords();
				
				TotalsCount();
			}
			
		}
		
		
		
		public function onFirstMessagesTimerTick(event:TimerEvent):void
		{
			Application.application.twitter.getTimeline();
			
			//Get schedueled Tweets
			Application.application.t_db.get_scheduled_tweets(Application.application._username);
			
			ScheduledTweets();
			
		}
		
		public function onFirstTimerTick(event:TimerEvent):void
		{
			
			//Application.application.ta.getTimeline();
			//get_groups
			
			//get_direct
			Application.application.twitter.get_direct_messages();
			
			//get_replies
			Application.application.twitter.get_replies();
			
			//filter links
			Application.application.filteractions.get_links();
			
			//filter keywords
			Application.application.filteractions.get_keywords();
			
			//Get Groups
			Application.application.groupactions.bind_group_titles();
			Application.application.groupactions.bind_global_group_members();
			Application.application.filteractions.get_group_status();
			
			//Get Counters
			Application.application.counter.get_totals();
			
			

			
		}
		
		/// Total Counters
		public function TotalsCount():void
		{
			var totals_timer:Timer = new Timer (30000, 0);
			
			totals_timer.addEventListener(TimerEvent.TIMER, onTotalsTimerTick);
			
			totals_timer.start();
		}
		
		public function onTotalsTimerTick(event:TimerEvent):void
		{
			Application.application.counter.get_totals();
		}
		
		public function Messages():void
		{
			var messages_timer:Timer = new Timer (_message_interval, 0);
			
			messages_timer.addEventListener(TimerEvent.TIMER, onMessageTimerTick);
			
			messages_timer.start();
		}
		
		public function onMessageTimerTick(event:TimerEvent):void
		{
			twitter.getTimeline();
			Application.application.ta.getTimeline();
		}
		
		public function ScheduledTweets():void
		{
			var scheduledtweet_timer:Timer = new Timer (60000, 0);
			
			scheduledtweet_timer.addEventListener(TimerEvent.TIMER, onScheduledTweetsTimerTick);
			
			scheduledtweet_timer.start();
		}
		
		public function onScheduledTweetsTimerTick(event:TimerEvent):void
		{
			// check tweets < date
			
			var currentDate:Date = new Date();
			
			for each(var o:ScheduledMessage in Application.application.ScheduledMessage)
			{
				if (currentDate>o.time && o.sending==false)
				{
					//Alert.show(o.time.toString());
					var ta:Actions = new Actions();
					
					Application.application.ScheduledMessage.itemUpdated(o, o.sending, false, true);
					
					Application.application.ta.sendTwitterStatus(o.message, true, o.id, null, null, null);
					
					
				}
				
			}
			
		}
		
		public function DirectMessage():void
		{
			var scheduledtweet_timer:Timer = new Timer (_direct_interval, 0);
			
			scheduledtweet_timer.addEventListener(TimerEvent.TIMER, onDirectMessagesTimerTick);
			
			scheduledtweet_timer.start();
		}
		
		public function onDirectMessagesTimerTick(event:TimerEvent):void
		{
			
			Application.application.twitter.get_direct_messages();
		}
		
		public function ReplyMessage():void
		{
			var scheduledtweet_timer:Timer = new Timer (_replies_interval, 0);
			
			scheduledtweet_timer.addEventListener(TimerEvent.TIMER, onReplyMessageTimerTick);
			
			scheduledtweet_timer.start();
		}
		
		public function onReplyMessageTimerTick(event:TimerEvent):void
		{
			
			Application.application.twitter.get_replies();
		}
		
		public function Groups():void
		{
			var scheduledtweet_timer:Timer = new Timer (_groups_interval, 0);
			
			scheduledtweet_timer.addEventListener(TimerEvent.TIMER, onGroupsTimerTick);
			
			scheduledtweet_timer.start();
		}
		
		public function onGroupsTimerTick(event:TimerEvent):void
		{
			
			Application.application.filteractions.get_group_status();
		}
		
		public function Keywords():void
		{
			var keywords_timer:Timer = new Timer (_groups_interval, 0);
			
			keywords_timer.addEventListener(TimerEvent.TIMER, onKeywordsTimerTick);
			
			keywords_timer.start();
		}
		
		public function onKeywordsTimerTick(event:TimerEvent):void
		{
			
			Application.application.filteractions.get_keywords();
		}
		
		
	}
}